﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using hou_elearning.Business;
using hou_elearning.Entity;
using System.ServiceModel.Activation;
using Newtonsoft.Json;
using System.ServiceModel.Web;

using hou_elearning.Custom;

/// <summary>
/// Thực hiện các hàm đã khai báo ở IHOULMSService
/// Trả lại các định dạng, hoặc thông báo khi thực hiện các hàm
/// </summary>
/// <author>liemqv</author>
/// <create>09/05/2012</create>
/// 

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class HOULMSService : IHOULMSService
{
    /// <summary>
    /// Các phương thức của Slide
    /// </summary>
    public IList<SlideEntity> SlideGetAll()
    {
        return SlideBRL.GetAll();
    }

    public SlideEntity SlideGetByPK(string PK_iSlideID)
    {
        Int64 PK_iID = Int64.Parse(PK_iSlideID);
        return SlideBRL.GetOne(PK_iID);
    }

    public Int64 SlideInsert(string strSlideJson)
    {
        SlideEntity oSlide = JsonConvert.DeserializeObject<SlideEntity>(strSlideJson);
        return SlideBRL.Add(oSlide);
    }

    public bool SlideUpdate(string strSlideJson)
    {
        SlideEntity oSlide = JsonConvert.DeserializeObject<SlideEntity>(strSlideJson);
        return SlideBRL.Edit(oSlide);
    }

    public bool SlideDelete(string PK_iSlideID)
    {
        Int64 PK_iID = Int64.Parse(PK_iSlideID);
        return SlideBRL.Remove(PK_iID);
    }

    /// <summary>
    /// Các phương thức của Trangslide
    /// </summary>
    public IList<TrangslideEntity> TrangslideGetAll()
    {
        return TrangslideBRL.GetAll();
    }

    public TrangslideEntity TrangslideGetByPK(string PK_iTrangslideID)
    {
        return TrangslideBRL.GetOne(Int64.Parse(PK_iTrangslideID));
    }

    public long TrangslideInsert(string strTrangslideJson)
    {
        TrangslideEntity oTrangsslide = JsonConvert.DeserializeObject<TrangslideEntity>(strTrangslideJson);
        return TrangslideBRL.Add(oTrangsslide);
    }

    public bool TrangslideUpdate(string strTrangslideJson)
    {
        TrangslideEntity oTrangsslide = JsonConvert.DeserializeObject<TrangslideEntity>(strTrangslideJson);
        return TrangslideBRL.Edit(oTrangsslide);
    }

    public bool TrangslideDelete(string PK_iTrangslideID)
    {
        return TrangslideBRL.Remove(Int64.Parse(PK_iTrangslideID));
    }


    public IList<TrangslideEntity> TrangslideGetByFK_iSlideID(string FK_iSlideID)
    {
        return TrangslideBRL.GetByFK_iSlideID(Int64.Parse(FK_iSlideID));
    }

    /// <summary>
    /// Các phương thức của Sukienslide
    /// </summary>
    public IList<SukienslideEntity> SukienslideGetAll()
    {
        return SukienslideBRL.GetAll();
    }

    public SukienslideEntity SukienslideGetByPK(string PK_sSukienslideID)
    {
        Int64 PK_iID = Int64.Parse(PK_sSukienslideID);
        return SukienslideBRL.GetOne(PK_iID);
    }

    public IList<SukienslideEntity> SukienslideGetByFK_iTrangslideID(string FK_iTrangslideID)
    {
        Int64 FK_iID = Int64.Parse(FK_iTrangslideID);
        return SukienslideBRL.GetByFK_iTrangslideID(FK_iID);
    }

    public long SukienslideInsert(string strSukienslideJson)
    {
        SukienslideEntity oSukienslide = JsonConvert.DeserializeObject<SukienslideEntity>(strSukienslideJson);
        return SukienslideBRL.Add(oSukienslide);
    }

    public bool SukienslideUpdate(string strSukienslideJson)
    {
        SukienslideEntity oSukienslide = JsonConvert.DeserializeObject<SukienslideEntity>(strSukienslideJson);
        return SukienslideBRL.Edit(oSukienslide);
    }

    public bool SukienslideDelete(string PK_iSukienslideID)
    {
        Int64 PK_iID = Int64.Parse(PK_iSukienslideID);
        return SukienslideBRL.Remove(PK_iID);
    }

    /// <summary>
    /// Các phương thức của Diemdanh
    /// </summary>
    public IList<DiemdanhEntity> DiemdanhGetAll()
    {
        return DiemdanhBRL.GetAll();
    }

    public DiemdanhEntity DiemdanhGetByPK(string PK_sDiemdanhID)
    {
        Int64 PK_iDiemdanhID = Int64.Parse(PK_sDiemdanhID);
        return DiemdanhBRL.GetOne(PK_iDiemdanhID);
    }

    public long DiemdanhInsert(String FK_iKhoahocID, String FK_iHocvienID, String FK_iTrangslideID)
    {
        WebOperationContext ctx = WebOperationContext.Current;
        System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK;
        long iRes = 0;
        DiemdanhEntity oDiemdanh = new DiemdanhEntity();
        oDiemdanh.FK_iHocvienID = Int64.Parse(FK_iHocvienID);
        oDiemdanh.FK_iKhoahocID = Int64.Parse(FK_iKhoahocID);
        oDiemdanh.FK_iTrangslideID = Int64.Parse(FK_iTrangslideID);
        try
        {
            //oDiemdanh = JsonConvert.DeserializeObject<DiemdanhEntity>(strDiemdanhJson);
            oDiemdanh.dThoidiemxem = DateTime.Now;
            oDiemdanh.iTrangthai = 1;
            if (DiemdanhBRL.GetByFK_iKhoahocIDAndFK_iHocvienIDAndFK_iTrangslideID(oDiemdanh.FK_iKhoahocID, oDiemdanh.FK_iHocvienID, oDiemdanh.FK_iTrangslideID).Count <= 0)
            {
                iRes = DiemdanhBRL.Add(oDiemdanh);
                ctx.OutgoingResponse.StatusCode = status;
            }
        }
        catch (Exception ex)
        {
            iRes = -1;
            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
        }
        return iRes;
    }

    public bool DiemdanhUpdate(string strDiemdanhJson)
    {
        DiemdanhEntity oDiemdanh = JsonConvert.DeserializeObject<DiemdanhEntity>(strDiemdanhJson);
        return DiemdanhBRL.Edit(oDiemdanh);
    }

    public bool DiemdanhDelete(string PK_sDiemdanhID)
    {
        Int64 PK_iDiemdanhID = Int64.Parse(PK_sDiemdanhID);
        return DiemdanhBRL.Remove(PK_iDiemdanhID);
    }

    /// <summary>
    /// Thống kê khóa học
    /// </summary>
    public IList<Thongkekhoahoc> Thongkekhoahoc(string PK_iKhoahocID, string PK_iHocvienID)
    {
        WebOperationContext ctx = WebOperationContext.Current;
        System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK;
        //System.IO.File.WriteAllText(@"E:\", "PK_iKhoahoc: " + PK_iKhoahocID + ", PK_iHocvienID: " + PK_iHocvienID);
        List<Thongkekhoahoc> lstThongkekhoahoc = new List<Thongkekhoahoc>();
        try
        {
            Int64 FK_iHocvienID = Int64.Parse(PK_iHocvienID);
            Int64 FK_iKhoahocID = Int64.Parse(PK_iKhoahocID);
            List<Khoahoc_BaihocEntity> lstKhoahocBaihoc = Khoahoc_BaihocBRL.GetByFK_iKhoahocID(FK_iKhoahocID);
            List<Baihoc_SlideEntity> lstBaihocSlide = new List<Baihoc_SlideEntity>();
            List<TrangslideEntity> lstTrangslide = new List<TrangslideEntity>();
            List<DiemdanhEntity> lstDiemdanh = new List<DiemdanhEntity>();
            List<ThongkeSlide> lstThongkeslide = null;
            foreach (Khoahoc_BaihocEntity oKhoahocBaihoc in lstKhoahocBaihoc)
            {
                Thongkekhoahoc oThongke = new Thongkekhoahoc();
                oThongke.sTenbaihoc = BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID).sTenbaihoc;
                lstBaihocSlide = Baihoc_SlideBRL.GetByFK_iBaihocID(oKhoahocBaihoc.FK_iBaihocID);
                lstThongkeslide = new List<ThongkeSlide>();
                foreach (Baihoc_SlideEntity oBaihocSlide in lstBaihocSlide)
                {
                    ThongkeSlide oThongkeslide = new ThongkeSlide();
                    oThongkeslide.sTenslide = SlideBRL.GetOne(oBaihocSlide.FK_iSlideID).sTenslide;
                    lstTrangslide = TrangslideBRL.GetByFK_iSlideID(oBaihocSlide.FK_iSlideID);
                    lstDiemdanh = DiemdanhBRL.GetByFK_iKhoahocIDAndFK_iHocvienID(FK_iKhoahocID, FK_iHocvienID);
                    int iSoluongdadoc = 0;
                    foreach (TrangslideEntity oTrangslide in lstTrangslide)
                    {
                        if (lstDiemdanh.Find(
                                delegate(DiemdanhEntity oDiemdanh)
                                {
                                    return oDiemdanh.FK_iTrangslideID == oTrangslide.PK_iTrangslideID;
                                }
                            ) != null)
                        {
                            iSoluongdadoc++;
                        }
                    }
                    if (iSoluongdadoc > 0)
                    {
                        int iPhantram = (iSoluongdadoc * 100) / lstTrangslide.Count;
                        oThongkeslide.sPhamtram = iPhantram + "";
                    }
                    else
                    {
                        oThongkeslide.sPhamtram = "0";
                    }
                    lstThongkeslide.Add(oThongkeslide);
                }
                oThongke.lstSlide = lstThongkeslide;
                lstThongkekhoahoc.Add(oThongke);
                ctx.OutgoingResponse.StatusCode = status;
            }
        }
        catch (Exception ex)
        {
            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
        }
        return lstThongkekhoahoc;
    }


    public string Test(string sYourName)
    {
        return "Hello " + sYourName + "!";
    }

    /// <summary>
    /// Khóa học theo XML
    /// </summary>
    public IList<KhoahocEntity> KhoahocGetAllXML()
    {
        return KhoahocBRL.GetAll();
    }

    public KhoahocEntity KhoahocGetByPKXML(string PK_iKhoahocID)
    {
        Int64 PK_iKhoahocID_ = Int64.Parse(PK_iKhoahocID);
        return KhoahocBRL.GetOne(PK_iKhoahocID_);
    }

    /// <summary>
    /// Lấy danh sách khóa học theo ID của học viên
    /// </summary>
    public IList<KhoahocEntity> KhoahocGetByFK_iNguoidungIDXML(string FK_iNguoidungID)
    {
        List<KhoahocEntity> lstKhoahoc = new List<KhoahocEntity>();
        Int64 PK_iNguoidungID = Int64.Parse(FK_iNguoidungID);
        return lstKhoahoc;
    }

    /// <summary>
    /// lấy danh sách bài học theo định dạng XML
    /// </summary>
    public IList<BaihocEntity> BaihocGetAllXML()
    {
        return BaihocBRL.GetAll();
    }

    public BaihocEntity BaihocGetByPKXML(string PK_iBaihocID)
    {
        Int64 PK_iBaihocID_ = Int64.Parse(PK_iBaihocID);
        return BaihocBRL.GetOne(PK_iBaihocID_);
    }

    public IList<BaihocEntity> BaihocGetByFK_iKhoahocIDXML(string FK_iKhoahocID)
    {
        Int64 FK_iKhoahocID_ = Int64.Parse(FK_iKhoahocID);
        List<Khoahoc_BaihocEntity> lstKhoahocBaihoc = Khoahoc_BaihocBRL.GetByFK_iKhoahocID(FK_iKhoahocID_);
        List<BaihocEntity> lstBaihoc = new List<BaihocEntity>();
        foreach(Khoahoc_BaihocEntity oKhoahocBaihoc in lstKhoahocBaihoc)
        {
            lstBaihoc.Add(BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID));
        }
        lstKhoahocBaihoc = null;
        return lstBaihoc;
    }


    /// <summary>
    /// lấy danh sách slide theo định dạng XML
    /// </summary>
    public IList<SlideEntity> SlideGetAllXML()
    {
        return SlideBRL.GetAll();
    }

    public SlideEntity SlideGetByPKXML(string PK_iSlideID)
    {
        Int64 PK_iSlideID_ = Int64.Parse(PK_iSlideID);
        return SlideBRL.GetOne(PK_iSlideID_);
    }

    public IList<SlideEntity> SlideGetByFK_iBaihocIDXML(string FK_iBaihocID)
    {
        Int64 FK_iKhoahocID_ = Int64.Parse(FK_iBaihocID);
        List<Baihoc_SlideEntity> lstBaihocSlide = Baihoc_SlideBRL.GetByFK_iBaihocID(FK_iKhoahocID_);
        List<SlideEntity> lstSlide = new List<SlideEntity>();
        foreach (Baihoc_SlideEntity oBaihocSlide in lstBaihocSlide)
        {
            lstSlide.Add(SlideBRL.GetOne(oBaihocSlide.FK_iSlideID));
        }
        lstBaihocSlide = null;
        return lstSlide;
    }

    /// <summary>
    /// lấy danh sách trang slide theo định dạng XML
    /// </summary>
    public IList<TrangslideEntity> TrangslideGetAllXML()
    {
        return TrangslideBRL.GetAll();
    }

    public IList<TrangslideEntity> TrangslideGetByFK_iSlideIDXML(string FK_iSlideID)
    {
        Int64 FK_iSlideID_ = Int64.Parse(FK_iSlideID);
        return TrangslideBRL.GetByFK_iSlideID(FK_iSlideID_);
    }

    /// <summary>
    /// lấy danh sách sự kiện slide theo định dạng XML
    /// </summary>
    public IList<SukienslideEntity> SukienslideGetAllXML()
    {
        return SukienslideBRL.GetAll();
    }

    public IList<SukienslideEntity> SukienslideGetByFK_iTrangslideIDXML(string FK_iTrangslideID)
    {
        Int64 FK_iTrangslideID_ = Int64.Parse(FK_iTrangslideID);
        return SukienslideBRL.GetByFK_iTrangslideID(FK_iTrangslideID_);
    }

    public IList<SukienslideEntity> SukienslideGetByFK_iSlideIDXML(string FK_iSlideID)
    {
        Int64 FK_iSlideID_ = Int64.Parse(FK_iSlideID);
        List<TrangslideEntity> lstTrangslide = TrangslideBRL.GetByFK_iSlideID(FK_iSlideID_);
        List<SukienslideEntity> lstSukienslide = new List<SukienslideEntity>();

        foreach(TrangslideEntity oTrangslide in lstTrangslide)
        {
            lstSukienslide.AddRange(SukienslideBRL.GetByFK_iTrangslideID(oTrangslide.PK_iTrangslideID));
        }

        return lstSukienslide;
    }
}
